//
//  WYCityController.m
//  Boss
//
//  Created by admin on 2018/7/16.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYCityController.h"
#import "CZHAddressPickerView.h"
#import "UIButton+CountDown.h"

#import "WYHeroAlertController.h"


@interface WYCityController ()
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIView *mainView;
@property (strong, nonatomic) IBOutlet UIView *view1;
@property (strong, nonatomic) IBOutlet UIView *view2;
@property (strong, nonatomic) IBOutlet UIView *view3;
@property (strong, nonatomic) IBOutlet UIView *view4;

@property (strong, nonatomic) IBOutlet UIView *view5;

@property (strong, nonatomic) IBOutlet UIView *view6;

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topHeight;


@property (nonatomic, copy) NSString *province;

@property (nonatomic, copy) NSString *city;

@property (nonatomic, copy) NSString *area;
@property (strong, nonatomic) IBOutlet UITextField *addressLabel;

@property (strong, nonatomic) IBOutlet UITextField *nameTextField;

@property (strong, nonatomic) IBOutlet UITextField *numberTextField;

@property (strong, nonatomic) IBOutlet UITextField *codeTextField;

@property (strong, nonatomic) IBOutlet UITextField *companyName;

@property (strong, nonatomic) IBOutlet UITextField *companyCase;

@property (strong, nonatomic) IBOutlet UIButton *codeBtn;

@property (strong, nonatomic) IBOutlet UILabel *label1;

@property (strong, nonatomic) IBOutlet UILabel *label2;

@property (strong, nonatomic) IBOutlet UILabel *label3;

@property (strong, nonatomic) IBOutlet UILabel *label4;
@property (strong, nonatomic) IBOutlet UILabel *label5;
@property (strong, nonatomic) IBOutlet UILabel *label6;

@property (nonatomic,strong) WYHeroAlertController *hac;



@end

@implementation WYCityController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    self.codeBtn.userInteractionEnabled = NO;
    // Do any additional setup after loading the view from its nib.
   



}
-(void)setupUI
{
    self.navigationItem.title = @"同城服务";
    self.topHeight.constant = self.navHeight;
    self.mainView.width = self.scrollView.width;
    self.mainView.height =750;
    self.nameTextField.borderStyle = UITextBorderStyleNone;
    
    
    
    
    
    
    
    [self.scrollView addSubview:self.mainView];
    self.scrollView.contentSize = self.mainView.size;
    
    [self.view1 setBorderWithWidth:kLineHeight andColor:[UIColor hexStringToColor:@"#e6e6e6" alpha:1.0]];
    [self.view2 setBorderWithWidth:kLineHeight andColor:[UIColor hexStringToColor:@"#e6e6e6" alpha:1.0]];
    [self.view3 setBorderWithWidth:kLineHeight andColor:[UIColor hexStringToColor:@"#e6e6e6" alpha:1.0]];
    [self.view4 setBorderWithWidth:kLineHeight andColor:[UIColor hexStringToColor:@"#e6e6e6" alpha:1.0]];
    [self.view5 setBorderWithWidth:kLineHeight andColor:[UIColor hexStringToColor:@"#e6e6e6" alpha:1.0]];
    [self.view6 setBorderWithWidth:kLineHeight andColor:[UIColor hexStringToColor:@"#e6e6e6" alpha:1.0]];
    [self.codeBtn setTitleColor:[UIColor hexStringToColor:@"#e38b78" alpha:1.0] forState:UIControlStateNormal];
    
    [self.numberTextField addTarget:self action:@selector(numberChangeed:) forControlEvents:(UIControlEventEditingChanged)];
    [self.nameTextField addTarget:self action:@selector(nameChangeed:) forControlEvents:(UIControlEventEditingChanged)];
    [self.codeTextField addTarget:self action:@selector(codeChangeed:) forControlEvents:(UIControlEventEditingChanged)];
    [self.companyCase addTarget:self action:@selector(companyCaseChangeed:) forControlEvents:(UIControlEventEditingChanged)];
    
    self.label1.attributedText = [self setLabelText:@"申请人姓名*"];
    self.label2.attributedText = [self setLabelText:@"申请人手机*"];
    self.label3.attributedText = [self setLabelText:@"手机验证码*"];
    self.label4.attributedText = [self setLabelText:@"所在区域*"];
    self.addressLabel.userInteractionEnabled = NO;
    self.label1.font = kFontSystemBIgger2(14);
    self.label2.font = kFontSystemBIgger2(14);
    self.label3.font = kFontSystemBIgger2(14);
    self.label4.font = kFontSystemBIgger2(14);
    self.label5.font = kFontSystemBIgger2(14);
    self.label6.font = kFontSystemBIgger2(14);
    
    self.nameTextField.font = kFontSystemBIgger2(14);
    self.numberTextField.font = kFontSystemBIgger2(14);
    self.codeTextField.font = kFontSystemBIgger2(14);
    self.addressLabel.font = kFontSystemBIgger2(14);
    self.companyCase.font = kFontSystemBIgger2(14);
    self.companyName.font = kFontSystemBIgger2(14);
    
}
-(NSAttributedString *)setLabelText:(NSString *)labelText
{
    NSMutableAttributedString *aString = [[NSMutableAttributedString alloc]initWithString:labelText];
    [aString addAttribute:NSForegroundColorAttributeName value:[UIColor hexStringToColor:@"#d8593f" alpha:1.0] range:NSMakeRange(labelText.length-1, 1)];
    [aString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15]range:NSMakeRange(labelText.length-1, 1)];
    return  aString;
}
-(void)companyCaseChangeed:(UITextField *)textField
{
    if (textField.text.length >= 50 ) {//输入完成
        textField.text = [textField.text substringToIndex:50];
    }
}



-(void)nameChangeed:(UITextField *)textField
{
    if (textField.text.length >= 6 ) {//输入完成
        textField.text = [textField.text substringToIndex:6];
    }
}
-(void)codeChangeed:(UITextField *)textField
{
    if (textField.text.length >= 6 ) {//输入完成
        textField.text = [textField.text substringToIndex:6];
    }
}

-(void)numberChangeed:(UITextField *)textField
{
    if (textField.text.length >= 11 ) {//输入完成
        textField.text = [textField.text substringToIndex:11];
        [self.codeBtn setTitleColor:kColorMainColor forState:UIControlStateNormal];
        self.codeBtn.userInteractionEnabled = YES;
    }else{
        [self.codeBtn setTitleColor:[UIColor hexStringToColor:@"#e38b78" alpha:1.0] forState:UIControlStateNormal];
        self.codeBtn.userInteractionEnabled = NO;
        
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)submissionAction:(id)sender {
   // [self toast:@"提交成功"];
    if (!self.nameTextField.text.length) {
        [self toast:@"请填写申请人姓名"];return;
    }
    if (!self.numberTextField.text.length) {
        [self toast:@"请填写申请人手机号"];return;
    }
    if (!self.codeTextField.text.length) {
         [self toast:@"请输入验证码"];return;
    }
    if (!self.addressLabel.text.length) {
        [self toast:@"请选择所在区域"];return;
    }
    WEAKSELF
    [WYHttpServer sendCitySerViceWithName:self.nameTextField.text WithPhone:self.numberTextField.text WithCode:self.codeTextField.text WithProvince:self.province WithCity:self.city WithCountry:self.area WithCompany:self.companyName.text WithCompanyCase:self.companyCase.text SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            [weakSelf toast:@"您的申请已提交，请耐心等待审核结果"];
        }else{
            [weakSelf toast:errorMessage];
        }
        
        
        
        
    }];
    
    
    
}
- (IBAction)clickCityBtn:(id)sender {
    
    WEAKSELF
    [CZHAddressPickerView areaPickerViewWithProvince:self.province city:self.city area:self.area areaBlock:^(NSString *province, NSString *city, NSString *area) {
        // CZHStrongS elf(self);
        weakSelf.province = province;
        weakSelf.city = city;
        weakSelf.area = area;
        weakSelf.addressLabel.text = [NSString stringWithFormat:@"%@ %@ %@",province,city,area];
    }];
    
    
}
- (IBAction)getCode:(id)sender {
      [self.codeBtn startWithTime:59 title:@"重新获取" countDownTitle:@"s后重新获取" mainColor:[UIColor whiteColor] countColor:kColorMainColor];
    [WYHttpServer sendMessageWithPhone:[self.numberTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""] SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
      
    }];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    
//    WYHeroAlertController *hac = [[WYHeroAlertController alloc]init];
//    self.hac = hac;
//    [[UIApplication sharedApplication].keyWindow addSubview:hac.view];
//    hac.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    
    
}


@end
